package so.shanku.essential.activity;

import android.os.Bundle;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.ProductCommentsAdapter;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.Constant;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Jaelyn on 16/3/3.
 * 商品评论
 */
public class ProductCommentsActivity extends BaseUIActivity {
    //商品评论
    @ViewInject(id = R.id.ptr_list)
    private PullToRefreshListView ptr_list;

    private ProductCommentsAdapter goodsCommentsAdapter;
    /**
     * 平乱
     */
    private ArrayList<JsonMap<String, Object>> dataList=new ArrayList<JsonMap<String, Object>>();
    private String goodsId;//发现id

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_comments_layout);
        goodsCommentsAdapter = new ProductCommentsAdapter(this);
        goodsCommentsAdapter.setDatas(dataList);
        ptr_list.setAdapter(goodsCommentsAdapter);

        ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
        ptr_list.setOnRefreshListener(loadMoreListener);

        goodsId = getIntent().getStringExtra(Constant.ID);
//        loadingToast.show();
//        getServerData(0);
    }

    /**
     * 刷新数据
     */
    private PullToRefreshBase.OnRefreshListener2 loadMoreListener = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            getServerData(1);
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            getServerData(2);
        }
    };

    /**
     * 获取服务器端数据
     * { "categoryId":0,"time": "","requestType":0}
     * @param requestType 请求类型
     */
    public void getServerData(int requestType) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("productId", goodsId);
        if(dataList.size()==0){
            requestType=0;
        }
        if(requestType==0) {
            params.put("time", "");
        } else if (requestType == 1) {
            params.put("time",dataList.get(0).getStringNoNull("createTime"));
        }else {
            params.put("time",dataList.get(dataList.size()-1).getStringNoNull("createTime"));
        }
        params.put("requestType",requestType);
        GetDataQueue queue = new GetDataQueue();
        queue.setTag(requestType);

        queue.setActionName(GetDataConfing.GetAction_GetProductCommentsList);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_GetProductCommentsList);
        getDataUtil.getData(queue);
    }



    private void validateData() {
//        if (commentsDatas == null || commentsDatas.isEmpty()) {
//            goods_comments_xlv.setVisibility(View.GONE);
//            nodataLayout.setVisibility(View.VISIBLE);
//        } else {
//            goods_comments_xlv.setVisibility(View.VISIBLE);
//            nodataLayout.setVisibility(View.GONE);
//        }
    }

    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

        @Override
        public void onLoaded(GetDataQueue entity) {
            ptr_list.onRefreshComplete();
            if (entity.isOk()) {
                if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
                    if (entity.getWhat() == GetDataConfing.What_GetProductCommentsList) {
                        ArrayList<JsonMap<String, Object>> temp_data = (ArrayList<JsonMap<String, Object>>) JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
                        setAdapterData(temp_data, (Integer) entity.getTag());
                        validateData();
                    }
                }
            } else {
                ShowGetDataError.showNetError(mContext);
            }
            loadingToast.dismiss();
        }

    };

    private void setAdapterData(ArrayList<JsonMap<String, Object>> temp_data, int requestType) {
        if (requestType == 0){
            if (temp_data.size() == 0) {
                ptr_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            } else {
                ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
                dataList.addAll(0, temp_data);
                goodsCommentsAdapter.setDatas(dataList);
                goodsCommentsAdapter.notifyDataSetChanged();
            }

        } else if (requestType == 1) {
            if (!temp_data.isEmpty()) {
                dataList.addAll(0, temp_data);
                goodsCommentsAdapter.setDatas(dataList);
                goodsCommentsAdapter.notifyDataSetChanged();
            }
        } else {
            if (temp_data.size() == 0) {
                ptr_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            } else {
                ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
            }

            if (!temp_data.isEmpty()) {
                dataList.addAll(temp_data);
                goodsCommentsAdapter.setDatas(dataList);
                goodsCommentsAdapter.notifyDataSetChanged();
            }
        }
    }

}